import { 
  getSuggest, cancel, unresolved, resolved
} from "../../api/complaints";
Page({
  data: {
    tabs: [
      { id: 1, title: '处理中' },
      { id: 2, title: '已处理' }
    ],
    activeTabId: 0,
    notWorkList: [],
    workList: [],
    imgList: [],
    undoModal: false,
    unsolvedModal: false,
    resolvedModal: false,
    undoBtns: [
      { text: '返回', extClass: 'cancelBtn'},
      { text: '确认撤销', extClass: 'buttonBold' },
    ],
    resolvedBtns: [
      { text: '返回', extClass: 'cancelBtn'},
      { text: '确认', extClass: 'buttonBold' },
    ],
    suggestId: '',
    content: '',
  },
  onLoad(option) {
    this.getData()
  },
  getData() {
    const type = this.data.activeTabId + 1
    getSuggest(type).then(res => {
      this.setData({
        notWorkList: res
      })
    })
  },
  handleTabClick({ index, tabsName }) {
    // console.log('切换', index, tabsName)
    this.setData({
      [tabsName]: index,
    });
    this.getData()
  },
  handleTabChange({ index, tabsName }) {
    this.setData({
      [tabsName]: index,
    });
  },
  checkPlan(e){
    my.navigateTo({
      url: '../complaintPogress/complaintPogress?suggestId=' + e.target.dataset.id
    });
  },
  onUndo(e){
    this.setData({
      suggestId: e.target.dataset.id,
      undoModal: true,
    });
  },
  onUndoClick(e) {
    const { target: { dataset } } = e;
    if(dataset.index == 0){
      this.setData({
        undoModal: false,
      });
    } else {
      cancel(this.data.suggestId).then(res =>{
        this.setData({
          undoModal: false,
        });
        this.getData()
      })
    }
  },
  onUnsolved(e){
    this.setData({
      suggestId: e.target.dataset.id,
      unsolvedModal: true,
    });
  },
  onUnSolvedClick(e) {
    const { target: { dataset } } = e;
    if(dataset.index == 0){
      this.setData({
        unsolvedModal: false,
      });
    } else {
      if(!this.data.content){
        my.showToast({
          type: 'none',
          content: '请输入补充内容',
          duration: 1500
        });
        return
      }
      unresolved({
        suggest_id: this.data.suggestId,
        content: this.data.content,
      }).then(res =>{
        this.setData({
          unsolvedModal: false,
        });
        this.getData()
      })
    }
  },
  onExplain(e) {
    this.setData({
      content: e.detail.value,
    });
  },
  onResolved(e){
    this.setData({
      suggestId: e.target.dataset.id,
      resolvedModal: true,
    });
  },
  onResolvedClick(e) {
    const { target: { dataset } } = e;
    if(dataset.index == 0){
      this.setData({
        resolvedModal: false,
      });
    } else {
      resolved(this.data.suggestId).then(res =>{
        this.setData({
          resolvedModal: false,
        });
        this.getData()
      })
    }
  },
});
